package com.launcher.wkang.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.launcher.wkang.App;
import com.launcher.wkang.activity.LauncherActivity1;
import com.launcher.wkang.fragment.Fragment1;
import com.launcher.wkang.fragment.Fragment2;
import com.launcher.wkang.fragment.Fragment3;
import com.launcher.wkang.fragment.Fragment4;
import com.launcher.wkang.fragment.Fragment5;
import com.launcher.wkang.util.PhoneUtil;
import com.launcher.wkang.R;
import android.widget.LinearLayout;

public class LauncherFragment extends FrameLayout {

    private float startX;
    private float endX;
    private int maxNum=5;
    private int screenWidth;
    private int currentNum;
    private int distance=100;
    private Fragment1 fragment1;
    private LinearLayout divisionLayout;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private Fragment5 fragment5;
    private LauncherActivity1 launcherActivity1;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    public LauncherFragment(Context context) {
        super(context);
        screenWidth=PhoneUtil.getWindowWidth(context);
        currentNum=0;
        this.launcherActivity1= (LauncherActivity1) context;
        setCurrentItem(0);
    }

    public LauncherFragment(Context context, AttributeSet attrs) {
        super(context, attrs);
        screenWidth=PhoneUtil.getWindowWidth(context);
        currentNum=0;
        this.launcherActivity1= (LauncherActivity1) context;

        setCurrentItem(0);

    }

    public Fragment1 getFragment1() {
        return fragment1;
    }

    public void setFragment1(Fragment1 fragment1) {
        this.fragment1 = fragment1;
    }

    public Fragment2 getFragment2() {
        return fragment2;
    }

    public void setFragment2(Fragment2 fragment2) {
        this.fragment2 = fragment2;
    }

    public Fragment3 getFragment3() {
        return fragment3;
    }

    public void setFragment3(Fragment3 fragment3) {
        this.fragment3 = fragment3;
    }

    public Fragment4 getFragment4() {
        return fragment4;
    }

    public void setFragment4(Fragment4 fragment4) {
        this.fragment4 = fragment4;
    }

    public Fragment5 getFragment5() {
        return fragment5;
    }

    public void setFragment5(Fragment5 fragment5) {
        this.fragment5 = fragment5;
    }






    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (App.getINstance().isProhibit){
            return false;
        }else {
            return super.dispatchTouchEvent(ev);
        }
    }
    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        // 先保存手指按下的x轴的坐标
        if (ev.getAction () == MotionEvent.ACTION_DOWN)
            startX = ev.getX ();
        if (ev.getAction () == MotionEvent.ACTION_MOVE) {
            /*
        计算手指移动时的坐标跟按下的坐标之间的绝对值，如果超过给定的值，
        就认为viewpager需要滚动。通过调节distance的大小，可以改变滑动
                灵敏度
              */
            if (Math.abs (ev.getX () - startX) < distance)
                return false;
            else
                // 意思就是：touch事件已经被PeopleViewPager自己消费了，不会传递到子控件
                return true;
        }
        // 其他情况，依旧保持默认的处理方法
        return super.onInterceptTouchEvent (ev);
    }
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            //当手指按下的时候
            startX = ev.getRawX();
        }else if(ev.getAction() == MotionEvent.ACTION_UP) {
            endX=ev.getRawX();
            if ((startX>endX)&&((startX-endX)>screenWidth/10)){
             currentNum++;
             setCurrentItem(currentNum);


            }else if ((endX>startX)&&((endX-startX)>screenWidth/10 )){
                currentNum--;
                setCurrentItem(currentNum);
            }
        }
        return true;
    }

    public void setCurrentItem(int itemNum){
        currentNum=itemNum;
        if (itemNum>maxNum-1){
            currentNum=0;
        }
        if (itemNum<0){
            currentNum=maxNum-1;
        }
        manager = this.launcherActivity1.getSupportFragmentManager();
        transaction = manager.beginTransaction();
        switch (currentNum){
            case 0:
                if (fragment1==null){
                    fragment1=new Fragment1();
                }
                transaction.replace(R.id.frame_content,fragment1);
                break;
            case 1:
                if (fragment2==null){
                    fragment2=new Fragment2();
                }
                transaction.replace(R.id.frame_content,fragment2);
                break;
            case 2:
                if (fragment3==null){
                    fragment3=new Fragment3();
                }
                transaction.replace(R.id.frame_content,fragment3);
                break;
            case 3:
                if (fragment4==null){
                    fragment4=new Fragment4();
                }
                transaction.replace(R.id.frame_content,fragment4);
                break;
            case 4:
                if (fragment5==null){
                    fragment5=new Fragment5();
                }
                transaction.replace(R.id.frame_content,fragment5);
                break;

        }
        transaction.commitAllowingStateLoss ();
        onPageSelected(currentNum);
    }


    public void onPageSelected(int i) {
        if (null==divisionLayout) {
            divisionLayout = this.launcherActivity1.findViewById(R.id.division_layout);
        }
        if (null!=divisionLayout) {
            for (int j = 0; j < divisionLayout.getChildCount(); j++) {
                ImageView imageView = (ImageView) divisionLayout.getChildAt(j);
                if (i == j) {
                    imageView.setImageResource(R.mipmap.selected);
                } else {
                    imageView.setImageResource(R.mipmap.unselected);
                }
            }
        }

    }




}
